﻿jQuery.toSpace = function (input) {
    if (input != null) {
        return input.replace("%20", " ");
    }
    return input;
};
jQuery.isNullOrEmpty = function (input) {
    if (input != null && input != "") {
        return false;
    }
    return true;
};
jQuery.getDate = function (yearDiff, monthDiff, dayDiff) {
    var date = new Date();
    if (yearDiff != null) {
        date.setYear(date.getFullYear() + yearDiff);
    }
    if (monthDiff != null) {
        date.setMonth(date.getMonth() + monthDiff);
    }
    if (dayDiff != null) {
        date.setDate(date.getDate() + dayDiff);
    }

    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
    return currentdate;
};

jQuery.getCurrentDate = function () {
    var date = new Date();
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
    return currentdate;
};
jQuery.getCurrentDateTime = function () {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
};
jQuery.prototype.serializeObject = function () {
    var obj = new Object();
    $.each(this.serializeArray(), function (index, param) {
        if (!(param.name in obj)) {
            obj[param.name] = param.value;
        }
    });
    return obj;
};
jQuery.prototype.serializeForm = function () {
    var result = new Object();
    $.each(this.find("input"), function (ix, obj) {
        var name = obj.name;
        if (name != null && name.length > 0) {
            var typeName = obj.type;
            if (typeName == "checkbox") {
                result[name] = obj.checked;
            } else if (typeName == "radio") {
                result[name] = obj.checked;
            } else {
                result[name] = obj.value;
            }
        }
    });
    $.each(this.find("select"), function (ix, obj) {
        var name = obj.name;
        if (name != null && name.length > 0) {
            result[name] = $(obj).val();
        }
    });
    return result;
};
jQuery.prototype.serializeParam = function () {
    var result = this.serialize();
    $.each(this.find("input[type='checkbox']"), function (ix, obj) {
        var name = obj.name;
        if (name != null && name.length > 0) {
            var oldStr = "&" + name + "=on";
            var newStr = "&" + name + "=" + obj.checked;
            result = result.replace(oldStr, "") + newStr;
        }
    });
    return result;
};
jQuery.downloadFile = function (url, method, fileName) {
    jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' +
                '<input type="text" name="fileName" value="' + fileName + '"/>' +
            '</form>')
    .appendTo('body').submit().remove();
};

/**
 * 
 * @param {} indexStr  例如:",1,2,"
 * @returns {} 
 */
jQuery.prototype.makeTableFilter = function (indexStr) {
    if ($(this).find("tbody tr").length > 0) {
        var cellLength = $(this).find("tbody tr:eq(0) td").length;
        var line = "<tr>";
        for (var i = 0; i < cellLength; i++) {
            if (indexStr.indexOf("," + i + ",") > -1) {
                line += "<td></td>";
            } else {
                var ctl = "<input class='form-control' name='filter_" + i + "' type='text'>";
                line += "<td>" + ctl + "</td>";
            }
        }
        line += "</tr>";

        $(line).insertBefore($(this).find("tbody tr:eq(0)"));
        //绑定过滤时事件
        var $tableObj = $(this);
        $(this).on("input", "input[name^=filter_]", function () {
            var $filterInputs = $tableObj.find("tbody input[name^=filter_]");
            $tableObj.find("tbody tr:gt(0)").each(function (rowIndex, rowItem) {
                var isShow = true;
                //几个条件结合判断
                $filterInputs.each(function (ix, filterInput) {
                    var filterValue = $(filterInput).val();
                    var cellIndex = $(filterInput).prop("name").replace("filter_", "");
                    var content = $(rowItem).find("td:eq(" + cellIndex + ")")[0].innerHTML;
                    if (content.indexOf(filterValue) == -1) {
                        isShow = false;
                        return;
                    }
                });

                if (isShow) {
                    $(rowItem).show();
                } else {
                    $(rowItem).hide();
                }
            });

        });
    }
};



(function ($) {
    var Obj = function () {
        this.BASE_URL = "http://localhost:8001";
    };
    Obj.prototype.ItemCode = {
        ServiceType: null,
        CrewType: null,
        Payment: null,
        UserUint: null,
        AllUnit: null
    };

    Obj.prototype.closeSelectUser = function (divId) {
        $("#" + divId).dialog("close");
    }
    Obj.prototype.openSelectUser = function (ctlId, divId) {
        $('#' + ctlId).bootstrapTable({
            url: window._util.BASE_URL + '/Business/QueryCrew',
            method: 'get',
            dataType: "json",
            cache: true,
            totalField: 'MaxRows',
            dataField: 'Items',
            queryParamsType: "Page",
            toolbar: '#selectUserToolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            singleSelect: true,
            pagination: true, //分页
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 20, 50, 100, 200, 500],        //可供选择的每页的行数（*）
            search: false, //显示搜索框
            sidePagination: "server", //服务端处理分页
            clickToSelect: true,                //是否启用点击选中行
            showRefresh: false,                  //是否显示刷新按钮
            queryParams: function (params) {
                params["Name"] = $("#" + divId + " .query-Name").val();
                params["Type"] = $("#" + divId + " .query-Type").val();
                params["Status"] = $("#" + divId + " .query-Status").val();
                params["Skill"] = $("#" + divId + " .query-Skill").val();
                return params;
            },
            columns: [
                {
                    checkbox: true,
                },
                {
                    field: 'Id',
                    title: '编号',
                    align: 'center',
                    hlign: 'center'
                },
                {
                    field: 'Name',
                    title: '姓名',
                    align: 'center',
                    hlign: 'center'
                },
                {
                    field: 'Gender',
                    title: '性别',
                    align: 'center',
                    hlign: 'center'
                },
                  {
                      field: 'Type',
                      title: '性质',
                      align: 'center',
                      hlign: 'center'
                  },
                {
                    field: 'Skill',
                    title: '技能',
                    align: 'center',
                    hlign: 'center'
                },
                  {
                      field: 'Status',
                      title: '状态',
                      align: 'center',
                      hlign: 'center'
                  },
                {
                    field: 'IdNo',
                    title: '身份证',
                    align: 'center',
                    hlign: 'center'
                },
                {
                    field: 'TrcNo',
                    title: '暂住证',
                    align: 'center',
                    hlign: 'center'
                },
                {
                    field: 'HealthNo',
                    title: '健康证',
                    align: 'center',
                    hlign: 'center'
                },
                {
                    field: 'LicenseNo',
                    title: '上岗证',
                    align: 'center',
                    hlign: 'center'
                },
                {
                    field: 'Tel',
                    title: '电话',
                    align: 'center',
                    hlign: 'center'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return '<button data-id="' + row.Id + '" class="select btn btn-success"><i class="fa fa-check fa-lg"></i> 选 择</button>';
                    }
                }
            ]
        });

        $("#" + divId).dialog("open");
        $("#" + ctlId + " tbody").click(function (event) {
            var element = $(event.target);
            if (element.is("button.select")) {
                var id = element.data("id");
                $.ajax({
                    url: window._util.BASE_URL + "/Business/GetCrew",
                    method: "GET",
                    dataType: "json",
                    data: "Id=" + id,
                    success: function (resp) {
                        if (resp != null) {
                            $("#editForm input.CrewId").val(id);
                            $("#editForm input.CrewName").val(resp.Name);
                            $("#" + divId).dialog("close");
                        } else {
                            alert("获取数据出错");
                        }
                    }
                }).fail(function (H, G) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            }
        });
    }

    window._util = new Obj();
})(jQuery)